package org.jeecg.modules.rental.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.iot.model.control.CentralControlConfig;
import org.jeecg.modules.rental.req.EBikeTrackReq;
import org.jeecg.modules.rental.service.ICtrlEBikeService;
import org.jeecg.modules.rental.service.IRentalAccountService;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags="操控电单车信息")
@RestController
@RequestMapping("/ctrl/ebike")
@RequiredArgsConstructor
public class CtrlEBikeController {

    private final ICtrlEBikeService ctrlEBikeService;

    private final IRentalAccountService rentalAccountService;

    //============================== 基于租车套餐的操作 ==============================

    @PostMapping("/pkg/on")
    @ApiOperation("上电")
    public Result powerOnByPkg(){
        return Result.OK("equ.poweron.success", ctrlEBikeService.powerOnByPkg());
    }

    @PostMapping("/pkg/off")
    @ApiOperation("断电")
    public Result powerOffByPkg(){
        return Result.OK("equ.poweroff.success", ctrlEBikeService.powerOffByPkg());
    }

    @PostMapping("/pkg/locked")
    @ApiOperation("设防")
    public Result lockedByPkg(){
        return Result.OK("equ.defense.success", ctrlEBikeService.lockedByPkg());
    }

    @PostMapping("/pkg/unlock")
    @ApiOperation("撤防")
    public Result unlockByPkg(){
        return Result.OK("equ.disarming.success", ctrlEBikeService.unlockByPkg());
    }

    @PostMapping("/pkg/search")
    @ApiOperation("寻车")
    public Result searchByPkg(){
        return Result.OK("寻车成功", ctrlEBikeService.searchByPkg());
    }

    @PostMapping("/pkg/unlock-seat")
    @ApiOperation("座位锁")
    public Result unlockSeatByPkg(){
        return Result.OK("打开座位锁", ctrlEBikeService.unlockSeatByPkg());
    }

    @PostMapping("/pkg/sport-mode/{enable}")
    @ApiOperation("运动模式")
    public Result setSportModeByPkg(@PathVariable Boolean enable){
        return Result.OK("运动模式", ctrlEBikeService.setSportModeByPkg(enable));
    }

    @PostMapping(value = "/pkg/track-list")
    @ApiOperation(value="车辆轨迹")
    public Result<String> trackListByPkg(@RequestBody EBikeTrackReq trackReq){
        String carTrackList = ctrlEBikeService.trackListBypkg(trackReq);
        return Result.OK("获取历史轨迹", carTrackList);
    }

//    @GetMapping(value = "/pkg/start-push")
//    @ApiOperation(value="开始推送电单车信息")
//    public Result<String> startPush() {
//        ctrlEBikeService.startPushByPkg();
//        return Result.OK("推送电单车信息");
//    }

    //============================== 基于租车账户(绑定车辆)的操作 ==============================

    @PostMapping("/account/on")
    @ApiOperation("上电")
    public Result powerOnByAccount(){
        return Result.OK("equ.poweron.success", ctrlEBikeService.powerOnByAccount());
    }

    @PostMapping("/account/off")
    @ApiOperation("断电")
    public Result powerOffByAccount(){
        return Result.OK("equ.poweroff.success", ctrlEBikeService.powerOffByAccount());
    }

    @PostMapping("/account/locked")
    @ApiOperation("设防")
    public Result lockedByAccount(){
        return Result.OK("equ.defense.success", ctrlEBikeService.lockedByAccount());
    }

    @PostMapping("/account/unlock")
    @ApiOperation("撤防")
    public Result unlockByAccount(){
        return Result.OK("equ.disarming.success", ctrlEBikeService.unlockByAccount());
    }

    @PostMapping("/account/search")
    @ApiOperation("寻车")
    public Result searchByAccount(){
        return Result.OK("寻车成功", ctrlEBikeService.searchByAccount());
    }

    @PostMapping("/account/unlock-seat")
    @ApiOperation("座位锁")
    public Result unlockSeatByAccount(){
        return Result.OK("打开座位锁", ctrlEBikeService.unlockSeatByAccount());
    }

    @PostMapping("/account/sport-mode/{enable}")
    @ApiOperation("运动模式")
    public Result setSportModeByAccount(@PathVariable Boolean enable){
        return Result.OK("运动模式", ctrlEBikeService.setSportModeByAccount(enable));
    }

    @PostMapping(value = "/account/track-list")
    @ApiOperation(value="车辆轨迹")
    public Result<String> trackListByAccount(@RequestBody EBikeTrackReq trackReq){
        String carTrackList = ctrlEBikeService.trackListByAccount(trackReq);
        return Result.OK("获取历史轨迹", carTrackList);
    }

//    @GetMapping(value = "/account/start-push")
//    @ApiOperation(value="开始推送电单车信息")
//    public Result<String> startPushByAccount() {
//        ctrlEBikeService.startPushByAccount();
//        return Result.OK("推送电单车信息");
//    }

    //============================== 直接操作车辆 ==============================

    @PostMapping("/on/{carCode}")
    @ApiOperation("上电")
    public Result powerOn(@PathVariable String carCode){
        return Result.OK("equ.poweron.success", ctrlEBikeService.powerOn(carCode));
    }

    @PostMapping("/off/{carCode}")
    @ApiOperation("断电")
    public Result powerOff(@PathVariable String carCode){
        return Result.OK("equ.poweroff.success", ctrlEBikeService.powerOff(carCode));
    }

    @PostMapping("/locked/{carCode}")
    @ApiOperation("设防")
    public Result locked(@PathVariable String carCode){
        return Result.OK("equ.defense.success", ctrlEBikeService.locked(carCode));
    }

    @PostMapping("/unlock/{carCode}")
    @ApiOperation("撤防")
    public Result unlock(@PathVariable String carCode){
        return Result.OK("equ.disarming.success", ctrlEBikeService.unlock(carCode));
    }

    @PostMapping("/search/{carCode}")
    @ApiOperation("寻车")
    public Result search(@PathVariable String carCode){
        return Result.OK("寻车成功", ctrlEBikeService.search(carCode));
    }

    @PostMapping("/unlock-seat/{carCode}")
    @ApiOperation("座位锁")
    public Result unlockSeat(@PathVariable String carCode){
        return Result.OK("打开座位锁", ctrlEBikeService.unlockSeat(carCode));
    }

    @PostMapping("/lockOpen/{carCode}")
    @ApiOperation("龙头锁(管理员)")
    public Result handleLockOpenManage(@PathVariable String carCode){
        return Result.OK("equ.defense.success", ctrlEBikeService.lockOpen(carCode));
    }

    @PostMapping("/lockClose/{carCode}")
    @ApiOperation("龙头锁关(管理员)")
    public Result handleLockCloseManage(@PathVariable String carCode){
        return Result.OK("equ.lock.close.success", ctrlEBikeService.lockClose(carCode));
    }

    @PostMapping("/sport-mode/{carCode}/{enable}")
    @ApiOperation("运动模式")
    public Result setSportMode(@PathVariable String carCode, @PathVariable Boolean enable){
        return Result.OK("运动模式", ctrlEBikeService.setSportMode(carCode, enable));
    }

    @GetMapping("/sport-mode/get") // 使用 GetMapping，因为是查询操作
    @ApiOperation("获取运动模式状态")
    public Result<Boolean> getSportModeStatus(@RequestParam(name = "carCode") String carCode, @RequestParam(name = "phone") String phone) {
        // 调用 equService 中的获取运动模式状态方法
        boolean isSportModeEnabled = ctrlEBikeService.getSportModeStatus(carCode, phone); //  假设 equService 中有这个方法
        return Result.OK("获取运动模式状态", isSportModeEnabled); // 直接返回布尔值
    }

    @PostMapping("/remote-ctrl/pair/{carCode}")
    @ApiOperation("遥控器配对")
    public Result pairRemoteControl(@PathVariable String carCode) {
        boolean isPaired = ctrlEBikeService.pairRemoteControl(carCode);
        return Result.OK(isPaired?"遥控器配对成功":"遥控器配对失败");
    }

    @PostMapping("/remote-ctrl/clear/{carCode}")
    @ApiOperation("清空遥控器")
    public Result clearRemoteControl(@PathVariable String carCode) {
        boolean isPaired = ctrlEBikeService.clearRemoteControl(carCode);
        return Result.OK(isPaired?"清空遥控器成功":"清空遥控器失败");
    }

    @PostMapping("/set-volume/{carCode}/{volume}")
    @ApiOperation("音量设置")
    public Result setVolume(@PathVariable String carCode, @PathVariable Integer volume){
        return Result.OK(ctrlEBikeService.setVolume(carCode, volume));
    }


    @PostMapping("/set-playaudio/{carCode}/{audioIndex}")
    @ApiOperation("播放音频")
    public Result setPlayAudio(@PathVariable String carCode, @PathVariable Integer audioIndex){
        return Result.OK(ctrlEBikeService.setPlayAudio(carCode, audioIndex.byteValue()));
    }

    @PostMapping(value = "/track-list/{carCode}")
    @ApiOperation(value="车辆轨迹")
    public Result<String> trackListByAccount(@RequestBody EBikeTrackReq trackReq, @PathVariable String carCode){
        String carTrackList = ctrlEBikeService.trackList(trackReq, carCode);
        return Result.OK("获取历史轨迹", carTrackList);
    }


    @GetMapping("/set-power/{carCode}")
    @ApiOperation("设置电池")
    public Result setPower(@PathVariable(name = "carCode") String carCode) {
        Integer seq = ctrlEBikeService.setPower(carCode);
        return Result.OK("设置电池成功，序列号：" + seq);
    }

    @GetMapping("/query-power/{carCode}")
    @ApiOperation("查询电池")
    public Result queryPower(@PathVariable(name = "carCode") String carCode) {
        Integer seq = ctrlEBikeService.queryPower(carCode);
        return Result.OK("查询电池，序列号：" + seq);
    }

    @GetMapping("/set-speed/{carCode}")
    @ApiOperation("设置车速")
    public Result setSpeed(@PathVariable(name = "carCode") String carCode) {
        return Result.OK("设置车速", ctrlEBikeService.setSpeed(carCode));
    }

    @GetMapping("/query-control/{carCode}")
    @ApiOperation("查询控制器")
    public Result queryControl(@PathVariable(name = "carCode") String carCode) {
        return Result.OK("查询控制器", ctrlEBikeService.queryControl(carCode));
    }

    @GetMapping("/set-control/{carCode}")
    @ApiOperation("配置控制器")
    public Result setCentralControlConfig(@PathVariable(name = "carCode") String carCode) {
        return Result.OK("配置控制器", ctrlEBikeService.setControl(carCode, new CentralControlConfig()));
    }

}
